package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import util.ConexionBD;
import excepcion.DAOExcepcion;
import modelo.mAdministrador;
import modelo.mBanco;
import modelo.mSabio;

public class daoSabio {
	
	public mSabio logUser(mSabio mSab) throws DAOExcepcion{
	    String resultado = "";
	    String query = "select idSabio, txNombre from sabio where txEmail = ? and txContrasena = ? and inEliminar = 0";
	    Connection con = null;
	    PreparedStatement stmt = null;
	    ResultSet rs = null;
	    try {
	      con = ConexionBD.obtenerConexion();
	      stmt = con.prepareStatement(query);
	      stmt.setString(1, mSab.getEmail());
	      stmt.setString(2, mSab.getPwd());
	      
	      rs = stmt.executeQuery();
	      if (rs.next()) {
	    	mSab.setIdSabio(rs.getInt(1));
	        resultado = rs.getString(2);
	        mSab.setNombre(resultado);
	      }

	    } catch (SQLException e) {
	      System.err.println(e.getMessage());
	      throw new DAOExcepcion(e.getMessage());
	    } finally {
	      /*this.cerrarResultSet(rs);
	      this.cerrarStatement(stmt);
	      this.cerrarConexion(con);*/
	    }
	    return mSab;
	  }
	
  public mSabio insertar(mSabio sa) throws DAOExcepcion {
    String query = "insert into sabio(txNombre,txApePateno, txApeMaterno, txApelativo, dtFecReg, txSexo, txResumen, txTipDoc, txNumDoc, txSitioWeb, txCelular, idBanco, txCtaCte, txDireccion, dtFecNac, txEmail, txContrasena) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
      con = ConexionBD.obtenerConexion();
      stmt = con.prepareStatement(query);
      stmt.setString(1, sa.getNombre());
      stmt.setString(2, sa.getApPaterno());
      stmt.setString(3, sa.getApMaterno());
      stmt.setString(4, sa.getApelativo());
      stmt.setString(5, sa.getFecReg());
      stmt.setString(6, sa.getSexo());
      stmt.setString(7, sa.getResumen());
      stmt.setString(8, sa.getTipDoc());
      stmt.setString(9, sa.getNumDoc());
      stmt.setString(10, sa.getSitioWeb());
      stmt.setString(11, sa.getCelular());
      stmt.setInt(12, sa.getIdBanco());
      stmt.setString(13, sa.getCtacte());
      stmt.setString(14, sa.getDireccion());
      stmt.setString(15, sa.getFecNac());
      stmt.setString(16, sa.getEmail());
      stmt.setString(17, sa.getPwd());
      
      
      int i = stmt.executeUpdate();
      if (i != 1) {
        throw new SQLException("No se pudo insertar");
      }
      // Obtener el ultimo id
      int id = 0;
      query = "select last_insert_id()";
      stmt = con.prepareStatement(query);
      rs = stmt.executeQuery();
      if (rs.next()) {
        id = rs.getInt(1);
      }
      sa.setIdSabio(id);

    } catch (SQLException e) {
      System.err.println(e.getMessage());
      throw new DAOExcepcion(e.getMessage());
    } finally {
      /*this.cerrarResultSet(rs);
      this.cerrarStatement(stmt);
      this.cerrarConexion(con);*/
    }
    return sa;
  }
  
  public int validaDNI(String dni) throws DAOExcepcion{
    int resultado = 0;
    String query = "select count(txNumDoc) from sabio where txNumDoc = ?";
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
      con = ConexionBD.obtenerConexion();
      stmt = con.prepareStatement(query);
      stmt.setString(1, dni);
      
      rs = stmt.executeQuery();
      if (rs.next()) {
        resultado = rs.getInt(1);
      }

    } catch (SQLException e) {
      System.err.println(e.getMessage());
      throw new DAOExcepcion(e.getMessage());
    } finally {
      /*this.cerrarResultSet(rs);
      this.cerrarStatement(stmt);
      this.cerrarConexion(con);*/
    }
    return resultado;
  }
  
  public int validaEmail(String email) throws DAOExcepcion{
    int resultado = 0;
    String query = "select count(txEmail) from sabio where txEmail = ?";
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
      con = ConexionBD.obtenerConexion();
      stmt = con.prepareStatement(query);
      stmt.setString(1, email);
      
      rs = stmt.executeQuery();
      if (rs.next()) {
        resultado = rs.getInt(1);
      }

    } catch (SQLException e) {
      System.err.println(e.getMessage());
      throw new DAOExcepcion(e.getMessage());
    } finally {
      /*this.cerrarResultSet(rs);
      this.cerrarStatement(stmt);
      this.cerrarConexion(con);*/
    }
    return resultado;
  }
  
  public Collection<mSabio> listar() throws DAOExcepcion {
	  Collection<mSabio> c = new ArrayList<mSabio>();
	  Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idSabio, txNombre, txApePateno, txApeMaterno,  case txTipDoc when 1 then 'DNI' when 2 then 'LE' when 3 then 'OTROS' end, txNumDoc, txEmail  from sabio where inEliminar = 0 order by idSabio";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				mSabio mSa = new mSabio();
				mSa.setIdSabio(rs.getInt(1));
				mSa.setNombre(rs.getString(2));
				mSa.setApPaterno(rs.getString(3));
				mSa.setApMaterno(rs.getString(4));
				mSa.setTipDoc(rs.getString(5));
				mSa.setNumDoc(rs.getString(6));
				mSa.setEmail(rs.getString(7));
				
				c.add(mSa);
			}
		
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			
		}
	  return c;
  }
}
